package com.lay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lay.entity.CarInsur;
import com.lay.entity.UserNumber;
import com.lay.entity.Users;
import com.lay.service.CarInsurService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/baoxian")
public class baoxianguanliController {
    @Autowired
    private CarInsurService carInsurService;

    @RequestMapping("/tobaoxian-add")
    public ModelAndView baoxianadd(){
        return new ModelAndView("product-add");
    }
    @RequestMapping("/carinsurAll")
    public ModelAndView carInsurAll(){
        ModelAndView mv = new ModelAndView();
        List<CarInsur> list = carInsurService.list(null);
        if (list.size() >0){
            mv.addObject("list",list);
            mv.addObject("count",list.size());
            mv.setViewName("article-list");
        }
        return mv;

    }
    @RequestMapping("/chazhao")
    @ResponseBody
    public String chazhao(String ci_name){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("ci_name",ci_name);
        CarInsur carInsur = carInsurService.getOne(qw);
        if (carInsur != null){
            return "0";
        }
        return "1";
    }
    @RequestMapping("/addBaoxian")
    public ModelAndView addBaoxian(CarInsur carInsur, HttpSession session){
        ModelAndView mv = new ModelAndView();
        UserNumber un = (UserNumber) session.getAttribute("users");
        Integer unId = un.getUnId();
        carInsur.setUnId(unId);
        carInsur.setCiState(0);
        boolean b = carInsurService.save(carInsur);
        if (b){
            mv.addObject("flag",b);
            mv.setViewName("product-add");
        }
        return mv;
    }
    @RequestMapping("/editState")
    @ResponseBody
    public String editStates(CarInsur carInsur){
        boolean b = carInsurService.updateById(carInsur);
        if (b){
            return "1";
        }
        return "0";
    }
}
